package com.eatme.web.sys.quartz.listener;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.listeners.TriggerListenerSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.Date;

public class AppTriggerListener extends TriggerListenerSupport {

    private static final Logger LOG = LoggerFactory.getLogger(AppTriggerListener.class);


    @Override
    public String getName() {
        return "appTriggerListener";
    }

    @Override
    public void triggerFired(Trigger trigger, JobExecutionContext context) {
        super.triggerFired(trigger, context);
        LOG.info("时间: {},trigger is fired!", DateFormatUtils.format(new Date(),DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
    }

    @Override
    public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
        super.triggerComplete(trigger, context, triggerInstructionCode);
        LOG.info("时间: {},trigger is complete,triggerInstructionCode: {}!", DateFormatUtils.format(new Date(),DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()),triggerInstructionCode);
    }

    @Override
    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
        LOG.info("时间: {},vetoJobExecution正在执行!", DateFormatUtils.format(new Date(),DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
        // 如果时间是5,15,25 尾数是5的时间点,就不执行
        Calendar now = Calendar.getInstance();
        int second = now.get(Calendar.SECOND);
//        if (second%10 == 5) {
//            LOG.info("时间: {},本次trigger不触发!", DateFormatUtils.format(new Date(),DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
//            return true;
//        }
//        return false;
        return super.vetoJobExecution(trigger, context);
    }
}
